<template>
  <div style="text-align: center;margin: 0 20px">
    <div style="margin-top: 150px">
      <div style="font-size: 25px;font-weight: bold">登录</div>
      <div style="font-size: 14px;color: grey">请输入用户名和密码进行登录</div>
    </div>
    <div style="margin-top: 50px">
      <el-form :model="form" :rules="rule" ref="formRef">
        <el-form-item prop="username">
          <el-input v-model="form.username" maxlength="10" type="text" placeholder="用户名/邮箱">
            <template #prefix>
              <el-icon><User/></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" maxlength="20" type="password" placeholder="密码">
            <template #prefix>
              <el-icon><Lock/></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12" style="text-align: left">
            <el-form-item prop="remember">
              <el-checkbox v-model="form.remember" label="记住我"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="text-align: right">
            <el-link  @click="router.push('/reset')">忘记密码?</el-link>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div style="margin-top: 40px">
      <el-button @click="userLogin" style="width: 270px;" type="success" plain>立即登录</el-button>
    </div>
    <el-divider>
      <span style="font-size: 13px;color: gray">没有账号</span>
    </el-divider>
    <div>
      <el-button @click="router.push('/register')" style="width: 270px;" type="warning" plain>立即注册</el-button>
    </div>
  </div>
</template>

<script setup>
import {reactive,ref} from "vue";
import {ElIcon, ElMessage} from 'element-plus';
import {User,Lock} from "@element-plus/icons-vue";
import {login,post} from "@/net/index.js";
import router from "@/router/index.js";



const formRef = ref();
const userInfo = ref()

const form = reactive({
  username: "",
  password: "",
  remember: false,
})
const rule = {
  username: [
    {required:true,message:'请输入用户名'}
  ],
  password: [
    {required:true,message:'请输入密码'}
  ]
}

function userLogin() {
  formRef.value.validate((valid) => {
    if (valid) {
      // 使用封装的 login 方法进行登录
      login(form.username, form.password, form.remember, async (loginData) => {
          // 登录成功后，尝试获取用户信息
          post(`/api/user/userByUsername/${form.username}`, null,
              async (userRes) => {
                // 用户信息获取成功
                window.localStorage.setItem('user', JSON.stringify(userRes));
                userInfo.value = userRes;

                // 根据角色跳转
                if (userInfo.value.role === 1) {
                  await router.push('/manager');
                } else if (userInfo.value.role === 3) {
                  await router.push('/user');
                }else if (userInfo.value.role === 2) {
                  await router.push('/team');
                }
              },
          );
      });
    }
  });
}
// function getUserInfo() {
//   return new Promise((resolve, reject) => {
//     // 先尝试获取用户信息
//     post(`/api/user/userByUsername/${form.username}`, null, (userRes) => {
//       if (userRes) {
//         window.localStorage.setItem('user', JSON.stringify(userRes))
//         userInfo.value = userRes
//         resolve(userRes)
//       } else {
//         // 用户不存在时，尝试获取团队信息
//         post(`/api/team/teamByUsername/${form.username}`, null, (teamRes) => {
//           if (teamRes) {
//             window.localStorage.setItem('user', JSON.stringify(teamRes))
//             userInfo.value = teamRes
//             resolve(teamRes)
//           } else {
//             reject('未找到用户信息')
//           }
//         })
//       }
//     })
//   })
// }
</script>

<style  scoped>

</style>